﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

using Common.Logging;

using Spring.Context;
using Spring.Context.Support;
using System.Web;
using System.Web.Routing;
using System.Web.SessionState;

namespace BOSS.Web
{
    /// <summary>
    /// Spring 控制器工厂
    /// </summary>
    class SpringControllerFactory : DefaultControllerFactory, IGlobalModule
    {
        /// <summary>
        /// 日志器
        /// </summary>
        private static readonly ILog logger = LogManager.GetLogger(typeof(SpringControllerFactory));

        private IControllerFactory m_defaultControllerFactory;

        #region IGlobalModule 成员

        /// <summary>
        /// 获取 模块名称
        /// </summary>
        public string Name
        {
            get { return "控制器工厂"; }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="application"></param>
        /// <param name="appContext"></param>
        public void Initialize(HttpApplication application, IApplicationContext appContext)
        {
            ControllerBuilder.Current.SetControllerFactory(this);
        }

        #endregion

        #region IControllerFactory 成员

        /// <summary>
        /// 创建控制器
        /// </summary>
        /// <param name="requestContext"></param>
        /// <param name="controllerName"></param>
        /// <returns></returns>
        public override IController CreateController(RequestContext requestContext, string controllerName)
        {
            try
            {
                IApplicationContext appContext = ContextRegistry.GetContext();
                string typeName = string.Format("{0}Controller", controllerName);
                IController controller = null;

                if (appContext.ContainsObject(typeName))
                {
                    controller = appContext.GetObject(typeName, typeof(IController)) as IController;
                }
                else
                {
                    if (this.m_defaultControllerFactory == null)
                    {
                        this.m_defaultControllerFactory = new DefaultControllerFactory();
                    }
                    controller = this.m_defaultControllerFactory.CreateController(requestContext, controllerName);
                }

                //logger.Debug(string.Format("CreateController: {0} - {1}", controllerName, controller));
                return controller;
            }
            catch (Exception ex)
            {
                logger.Error(string.Format("CreateController: {0} - FAILED", controllerName), ex);
                return null;
            }
        }

        /// <summary>
        /// 销毁控制器
        /// </summary>
        /// <param name="controller"></param>
        public override void ReleaseController(IController controller)
        {
            try
            {
                IApplicationContext appContext = ContextRegistry.GetContext();
                if (appContext.ContainsObject(controller.GetType().Name))
                {
                    if (controller is IDisposable)
                    {
                        ((IDisposable)controller).Dispose();
                    }
                }
                else
                {
                    if (this.m_defaultControllerFactory == null)
                    {
                        this.m_defaultControllerFactory = new DefaultControllerFactory();
                    }
                    this.m_defaultControllerFactory.ReleaseController(controller);
                }
                //logger.Debug(string.Format("ReleaseController: {0}", controller));
            }
            catch (Exception ex)
            {
                logger.Debug(string.Format("ReleaseController: {0} - FAILED", controller), ex);
            }
        }

        #endregion
    }
}
